﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace letGo.Control
{
    public partial class FillInfoControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                //Msg.Text = "请先登录";
                //Response.Write("<meta http-equiv=refresh content='0.1;URL=../Account/Regist.aspx'>");
            }
        }



        protected class FileInfo
        {
            public string FileName = "";
            public string Type = "";
            public string Size = "";
            public string FilePath="";
            public string UploadTime = "";
        }
        //protected UserLoginInfo _UserLoginInfo = new UserLoginInfo();//创建对象
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            Msg.Text = "";
            FileInfo file = new FileInfo();

            if (FileUpload1.HasFile)
            {
                DateTime dt = DateTime.Now;
                string filename=dt.Year.ToString()+dt.Month.ToString()+dt.Day.ToString()+dt.Hour.ToString()+dt.Minute.ToString()+dt.Second.ToString()+Session["username"];
                string flag = Path.GetExtension(FileUpload1.PostedFile.FileName);
                string path = Server.MapPath("../Files/" + filename+flag);
                float size = (FileUpload1.PostedFile.ContentLength / 1024);

                if(size<=1024)
                {
                    if(flag==".bmp"||flag==".jpg"||flag==".jpeg")
                    {
                        try
                        {

                            DateTime upload = DateTime.Now;
                            FileUpload1.SaveAs(path);
                            file.Type = flag;//获得文件格式
                            file.Size = size.ToString();//文件大小
                            file.FilePath = path;//上传到服务器的绝对路径
                            file.UploadTime = DateTime.Now.ToShortDateString();//上传时间
                            //handler.AddFile(file);//将上传的文件信息保存到数据库
                            Msg.Text = "上传成功！";
                            HeadImage.ImageUrl = "../Files/" + filename + flag;
                        }
                        catch
                        {

                            Msg.Text = "上传失败！";
                        }
                    }
                        else{
                            Msg.Text = "文件格式不匹配！";
                        }
                }
                else{

                    Msg.Text = "文件大于1M！";
                
                }
            }
        }


        
    }
}